package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.api.AppService;
import com.shanjupay.merchant.api.dto.AppDTO;
import com.shanjupay.merchant.convert.AppCovert;
import com.shanjupay.merchant.entity.App;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.AppMapper;
import com.shanjupay.merchant.mapper.MerchantMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * @author whb
 * @create 2021-12-04 14:46
 * @desciption app应用管理相关Service实现类
 */
@Service
public class AppServiceImpl implements AppService {
    @Autowired
    private AppMapper appMapper;
    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 创建app应用
     * @param merchantId 所属商户id
     * @param appDTO app应用信息
     * @return 创建成功的app应用信息
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
        //校验传入参数是否符合
        if (merchantId==null || appDTO==null || StringUtils.isBlank(appDTO.getAppName())){
            throw new BusinessException(CommonErrorCode.E_300009);
        }

        //校验商户是否通过资质审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        //商户不存在
        if (merchant==null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }

        //商户资质申请尚未通过
        if (!"2".equals(merchant.getAuditStatus())){
            throw new BusinessException(CommonErrorCode.E_200003);
        }

        //校验应用名称唯一性
        String appName = appDTO.getAppName();
        Boolean existAppName = isExistAppName(appName);
        if (existAppName){
            throw new BusinessException(CommonErrorCode.E_200004);
        }

        String appId = UUID.randomUUID().toString();//生成应用id
        App app = AppCovert.INSTANCE.dto2entity(appDTO);//将dto转换成entity
        app.setAppId(appId);//设置app应用id
        app.setMerchantId(merchantId);//给应用绑定商户
        appMapper.insert(app);//将app应用数据保存到app表中

        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     * 根据商户id查询app应用列表
     * @param merchantId 商户id
     * @return 商户id下对应的app应用列表
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppByMerchantId(Long merchantId) throws BusinessException {
        List<App> appList = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppCovert.INSTANCE.entityList2dtoList(appList);
    }

    /**
     * 根据应用id查询app应用信息
     * @param appId 应用id
     * @return 应用对象
     * @throws BusinessException
     */
    @Override
    public AppDTO queryAppById(String appId) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, appId));
        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     * 判断应用名称是否存在
     * @param appName
     * @return
     */
    private Boolean isExistAppName(String appName) {
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appName));
        return count>0;
    }
}
